<?php

App::uses('AppController', 'Controller');

/**
 * Admins Controller
 *
 */
class AdminsController extends AppController {

    /**
     * Scaffold
     *
     * @var mixed
     */
    public $scaffold;
    public $uses = array('UserGroup', 'User');

    public function beforeFilter() {
        parent::beforeFilter();
//        if (! $this->Session->check('User')) {
//            $this->redirect('index');
//        }
        $this->layout = "default_admin";
    }
    
    public function index() {
        if ($this->Session->check('User')) {
            $this->redirect('controlPanel');
        }
         $this->layout = "default_blank";
        if ($this->request->is('post')) {
            $this->User->set($this->request->data['User']);
            $validator = $this->User->validator();
            unset($validator['email']['dupplicate']);
            unset($validator['password']['passwordLength']);
            if ($this->User->validates(array('fieldList' => array('email', 'password')))) {
                $email = $this->request->data['User']['email'];
                $password = $this->request->data['User']['password'];
                $data = $this->User->val($email, $password);
                //debug($data)
                if ($data != NULL) {
                    $this->Session->write('User.id', $data['User']['id']);
                    $this->Session->write('User.email', $data['User']['email']);
                    $this->Session->write('User.first_name', $data['User']['first_name']);
                    $this->Session->write('User.last_name', $data['User']['last_name']);
                    $this->Session->write('User.username', $data['User']['username']);
                    $this->redirect('/Admins');
                }
            }
        }
    }
    
    public function logout() {
        $this->autoRender = false;
        //$this->Session->delete('User');
        SessionComponent::destroy();
        $this->redirect('index');
    }
    
    public function controlPanel(){
        
    }

}
